<?php
// $Id: $

include_once('BaseModel.class.php');
class ManageUserRightModel extends BaseModel
{
    function get_priv($privilege,$privilege_level)
    {
        $sql  = "SELECT module_name as 'priv' 
                FROM `user_rights`
                JOIN user_modules ON user_modules.modules_id=user_rights.modules_id
                LEFT JOIN usertype ON user_rights.usertype_id=usertype.id
                WHERE user_rights.active=1 AND LOWER(usertype.usertype_name) LIKE  LOWER('$privilege%') AND user_rights.usertypelevel_id='$privilege_level'";
        return $this->query($sql, true);
            
    }

    function getlist ($usertype,$usertype_level)
    {
		$query = "SELECT um.*,ur.user_right_id,ur.active,ut.id,umg.modulegroup_name as type
                          FROM `user_modules` um 
                          JOIN `user_modulegroups` umg ON um.modulegroup_id = umg.id 
                          LEFT JOIN usertype ut ON um.usertype_id = ut.id 
                          LEFT JOIN user_rights ur ON um.modules_id = ur.modules_id 
                          WHERE ut.id = '$usertype' AND ur.usertypelevel_id= '$usertype_level' AND ur.usertype_id <>''";	
	    if($usertype && $usertype_level) $this->addmodule($usertype,$usertype_level);
        return $this->query($query, true);
    }


    function usertypelist ()
    {
        $query = "SELECT id,usertype_name as 'name' from usertype WHERE active='1'";
        return $this->query($query, true);
    }
    function usertypelevellist ()
    {
        $query = "SELECT id,usertypelevel_name as 'name' from usertype_levels WHERE active='1'";
        return $this->query($query, true);
    }
	
    function moduletypelist ($priv)
    {
        $query = "SELECT DISTINCT(user_modulegroups.modulegroup_name)as type FROM user_modules
                 JOIN user_modulegroups ON user_modules.modulegroup_id = user_modulegroups.id WHERE usertype_id ='$priv'";
        return $this->query($query, true);
    }
	
	function addmodule($id1,$id2)
	{
			$query = "INSERT INTO user_rights (usertype_id,usertypelevel_id,modules_id) 
						(SELECT '$id1','$id2',modules_id FROM user_modules WHERE modules_id NOT IN 
						(SELECT modules_id FROM user_rights WHERE usertype_id='$id1' AND usertypelevel_id='$id2')AND usertype_id = '$id1');";
        return $this->query($query, false);
	}


    function clearlist($id1,$id2)
    {
			
        $query = "UPDATE user_rights SET active=0 WHERE usertype_id='$id1' AND usertypelevel_id='$id2'";
        return $this->query($query, false);
										
    }

    function updateme($id)
	{
        $query = "UPDATE user_rights SET active=1 WHERE user_right_id=$id";
        return $this->query($query, false);
    }
}

?>
